package com.cms.tags;

/**
 * <p>Title: ECCM</p>
 *
 * <p>Description: Enterprise Collaboration, Customer, and Resources
 * Management</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: eConage Software</p>
 *
 * @author not attributable
 * @version 1.0
 */
import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
import org.apache.taglibs.i18n.ResourceHelper;

public class PaginationButtonTag extends TagSupport {

	private final static Logger logger = Logger
			.getLogger(PaginationButtonTag.class);

	int itemTotal;
	int itemPerPage;
	int currentPage;

	public void setItemTotal(int itemTotal) {
		this.itemTotal = itemTotal;
	}

	public void setItemPerPage(int itemPerPage) {
		this.itemPerPage = itemPerPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int doStartTag() throws JspException {
		try {

			ResourceBundle bundle = ResourceHelper.getBundle(pageContext);

			JspWriter out = pageContext.getOut();
			// int pageTotal = (int)((double)itemTotal/itemPerPage + 0.5);
			int pageTotal = 0;
			double tempnum = (double) itemTotal / itemPerPage;
			if ((tempnum - (int) tempnum) == 0)
				pageTotal = (int) tempnum;
			else
				pageTotal = (int) tempnum + 1;
			out.print("<b>");
			out.print(bundle.getString("total"));
			out.print("</b>:");
			out.print(itemTotal);
			out.print("&nbsp;;");
			out.print(bundle.getString("active"));
			out.print(":<span style=\"font-weight:bold;color:red;font-size:12px;\">");
			out.print(itemPerPage * (currentPage - 1) + 1);
			out.print("-");
			out.print(currentPage == pageTotal ? itemTotal : itemPerPage
					* currentPage);
			out.print("</span>");

			out.print("<span style=\"width:10px;\">&nbsp;</span>");
			if (currentPage > 1) {
				out.print("<a href=\"javascript:getListByPageNum('");
				out.print(currentPage - 1);
				out.print("')\"><img src=\"../img/newicons/arrow_left.gif\" border=\"0\" height=\"16\" style=\"vertical-align:bottom;\">");
				out.print(bundle.getString("prev_page"));
				out.print("</a>");
			}

			if (currentPage < pageTotal) {
				out.print("<a href=\"javascript:getListByPageNum('");
				out.print(currentPage + 1);
				out.print("')\"><img src=\"../img/newicons/arrow_right.gif\" border=\"0\" height=\"16\" style=\"vertical-align:bottom;\">");
				out.print(bundle.getString("next_page"));
				out.print("</a>");
			}

		} catch (IOException ioe) {
			throw new JspException("Error: IOException while writing to client"
					+ ioe.getMessage());
		}

		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
}
